home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2001 June
/
CHIP Haziran 2001.iso
/
prog
/
haziran
/
19
/
setup.exe
/
data.z
/
p9030_lib.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-04-11
|
26KB
|
882 lines
////////////////////////////////////////////////////////////////
// File - P9030_LIB.C
//
// Library for 'WinDriver for PLX 9030' API.
// The basic idea is to get a handle for the board
// with P9030_Open() and use it in the rest of the program
// when calling WD functions. Call P9030_Close() when done.
//
////////////////////////////////////////////////////////////////
#include "p9030_lib.h"
#include "../../../include/windrvr_int_thread.h"
#include <stdio.h>
// this string is set to an error message, if one occurs
CHAR P9030_ErrorString[1024];
// internal data structures
typedef struct
{
WD_INTERRUPT Int;
HANDLE hThread;
WD_TRANSFER Trans[2];
P9030_INT_HANDLER funcIntHandler;
} P9030_INTERRUPT;
typedef struct
{
DWORD dwLocalBase;
DWORD dwMask;
DWORD dwBytes;
DWORD dwAddr;
DWORD dwAddrDirect;
BOOL fIsMemory;
} P9030_ADDR_DESC;
typedef struct P9030_STRUCT
{
HANDLE hWD;
WD_CARD cardLock;
WD_PCI_SLOT pciSlot;
WD_CARD_REGISTER cardReg;
P9030_ADDR_DESC addrDesc[AD_PCI_BARS];
BOOL fUseInt;
P9030_INTERRUPT Int;
} P9030_STRUCT;
// internal function used by P9030_Open()
BOOL P9030_DetectCardElements(P9030_HANDLE hPlx);
// internal function used by P9030_Read... and P9030_Write... functions
void P9030_SetMode (P9030_HANDLE hPlx, P9030_ADDR addrSpace, P9030_MODE mode, DWORD dwLocalAddr);
DWORD P9030_CountCards (DWORD dwVendorID, DWORD dwDeviceID)
{
WD_VERSION ver;
WD_PCI_SCAN_CARDS pciScan;
HANDLE hWD = INVALID_HANDLE_VALUE;
P9030_ErrorString[0] = '\0';
hWD = WD_Open();
// check if handle valid & version OK
if (hWD==INVALID_HANDLE_VALUE)
{
sprintf( P9030_ErrorString, "Failed opening " WD_PROD_NAME " device\n");
return 0;
}
BZERO(ver);
WD_Version(hWD,&ver);
if (ver.dwVer<WD_VER)
{
sprintf( P9030_ErrorString, "Incorrect " WD_PROD_NAME " version\n");
WD_Close (hWD);
return 0;
}
BZERO(pciScan);
pciScan.searchId.dwVendorId = dwVendorID;
pciScan.searchId.dwDeviceId = dwDeviceID;
WD_PciScanCards (hWD, &pciScan);
WD_Close (hWD);
if (pciScan.dwCards==0)
sprintf( P9030_ErrorString, "no cards found\n");
return pciScan.dwCards;
}
BOOL P9030_Open (P9030_HANDLE *phPlx, DWORD dwVendorID, DWORD dwDeviceID, DWORD nCardNum, DWORD options)
{
P9030_HANDLE hPlx = (P9030_HANDLE) malloc (sizeof (P9030_STRUCT));
WD_VERSION ver;
WD_PCI_SCAN_CARDS pciScan;
WD_PCI_CARD_INFO pciCardInfo;
*phPlx = NULL;
P9030_ErrorString[0] = '\0';
BZERO(*hPlx);
hPlx->cardReg.hCard = 0;
hPlx->hWD = WD_Open();
// check if handle valid & version OK
if (hPlx->hWD==INVALID_HANDLE_VALUE)
{
sprintf( P9030_ErrorString, "Failed opening " WD_PROD_NAME " device\n");
goto Exit;
}
BZERO(ver);
WD_Version(hPlx->hWD,&ver);
if (ver.dwVer<WD_VER)
{
sprintf( P9030_ErrorString, "Incorrect " WD_PROD_NAME " version\n");
goto Exit;
}
BZERO(pciScan);
pciScan.searchId.dwVendorId = dwVendorID;
pciScan.searchId.dwDeviceId = dwDeviceID;
WD_PciScanCards (hPlx->hWD, &pciScan);
if (pciScan.dwCards==0) // Found at least one card
{
sprintf( P9030_ErrorString, "Could not find PCI card\n");
goto Exit;
}
if (pciScan.dwCards<=nCardNum)
{
sprintf( P9030_ErrorString, "Card out of range of available cards\n");
goto Exit;
}
BZERO(pciCardInfo);
pciCardInfo.pciSlot = pciScan.cardSlot[nCardNum];
hPlx->pciSlot = pciCardInfo.pciSlot;
WD_PciGetCardInfo (hPlx->hWD, &pciCardInfo);
hPlx->cardReg.Card = pciCardInfo.Card;
hPlx->fUseInt = (options & P9030_OPEN_USE_INT) ? TRUE : FALSE;
if (!hPlx->fUseInt)
{
DWORD i;
// Remove interrupt item if not needed
for (i=0; i<hPlx->cardReg.Card.dwItems; i++)
{
WD_ITEMS *pItem = &hPlx->cardReg.Card.Item[i];
if (pItem->item==ITEM_INTERRUPT)
pItem->item = ITEM_NONE;
}
}
else
{
DWORD i;
// make interrupt resource sharable
for (i=0; i<hPlx->cardReg.Card.dwItems; i++)
{
WD_ITEMS *pItem = &hPlx->cardReg.Card.Item[i];
if (pItem->item==ITEM_INTERRUPT)
pItem->fNotSharable = FALSE;
}
}
hPlx->cardReg.fCheckLockOnly = FALSE;
WD_CardRegister (hPlx->hWD, &hPlx->cardReg);
if (hPlx->cardReg.hCard==0)
{
sprintf ( P9030_ErrorString, "Failed locking device\n");
goto Exit;
}
if (!P9030_DetectCardElements(hPlx))
{
sprintf ( P9030_ErrorString, "Card does not have all items expected for PLX 9030\n");
goto Exit;
}
// Open finished OK
*phPlx = hPlx;
return TRUE;
Exit:
// Error durin Open
if (hPlx->cardReg.hCard)
WD_CardUnregister(hPlx->hWD, &hPlx->cardReg);
if (hPlx->hWD!=INVALID_HANDLE_VALUE)
WD_Close(hPlx->hWD);
free (hPlx);
return FALSE;
}
void P9030_GetPciSlot(P9030_HANDLE hPlx, WD_PCI_SLOT *pPciSlot)
{
*pPciSlot = hPlx->pciSlot;
}
DWORD P9030_ReadPCIReg(P9030_HANDLE hPlx, DWORD dwReg)
{
WD_PCI_CONFIG_DUMP pciCnf;
DWORD dwVal;
BZERO (pciCnf);
pciCnf.pciSlot = hPlx->pciSlot;
pciCnf.pBuffer = &dwVal;
pciCnf.dwOffset = dwReg;
pciCnf.dwBytes = 4;
pciCnf.fIsRead = TRUE;
WD_PciConfigDump(hPlx->hWD,&pciCnf);
return dwVal;
}
void P9030_WritePCIReg(P9030_HANDLE hPlx, DWORD dwReg, DWORD dwData)
{
WD_PCI_CONFIG_DUMP pciCnf;
BZERO (pciCnf);
pciCnf.pciSlot = hPlx->pciSlot;
pciCnf.pBuffer = &dwData;
pciCnf.dwOffset = dwReg;
pciCnf.dwBytes = 4;
pciCnf.fIsRead = FALSE;
WD_PciConfigDump(hPlx->hWD,&pciCnf);
}
BOOL P9030_DetectCardElements(P9030_HANDLE hPlx)
{
DWORD i;
DWORD ad_sp;
BZERO(hPlx->Int);
BZERO(hPlx->addrDesc);
for (i=0; i<hPlx->cardReg.Card.dwItems; i++)
{
WD_ITEMS *pItem = &hPlx->cardReg.Card.Item[i];
switch (pItem->item)
{
case ITEM_MEMORY:
case ITEM_IO:
{
DWORD dwBytes;
DWORD dwAddr;
DWORD dwAddrDirect = 0;
DWORD dwPhysAddr;
BOOL fIsMemory;
if (pItem->item==ITEM_MEMORY)
{
dwBytes = pItem->I.Mem.dwBytes;
dwAddr = pItem->I.Mem.dwTransAddr;
dwAddrDirect = pItem->I.Mem.dwUserDirectAddr;
dwPhysAddr = pItem->I.Mem.dwPhysicalAddr;
fIsMemory = TRUE;
}
else
{
dwBytes = pItem->I.IO.dwBytes;
dwAddr = pItem->I.IO.dwAddr;
dwPhysAddr = dwAddr & 0xffff;
fIsMemory = FALSE;
}
for (ad_sp=P9030_ADDR_REG; ad_sp<=P9030_ADDR_EPROM; ad_sp++)
{
DWORD dwPCIAddr;
DWORD dwPCIReg;
if (hPlx->addrDesc[ad_sp].dwAddr) continue;
if (ad_sp==P9030_ADDR_REG) dwPCIReg = PCI_BAR0;
else if (ad_sp<P9030_ADDR_EPROM)
dwPCIReg = PCI_BAR2 + 4*(ad_sp-P9030_ADDR_SPACE0);
else dwPCIReg = PCI_ERBAR;
dwPCIAddr = P9030_ReadPCIReg(hPlx, dwPCIReg);
if (dwPCIAddr & 1)
{
if (fIsMemory) continue;
dwPCIAddr &= ~0x3;
}
else
{
if (!fIsMemory) continue;
dwPCIAddr &= ~0xf;
}
if (dwPCIAddr==dwPhysAddr)
break;
}
if (ad_sp<=P9030_ADDR_EPROM)
{
DWORD j;
hPlx->addrDesc[ad_sp].dwBytes = dwBytes;
hPlx->addrDesc[ad_sp].dwAddr = dwAddr;
hPlx->addrDesc[ad_sp].dwAddrDirect = dwAddrDirect;
hPlx->addrDesc[ad_sp].fIsMemory = fIsMemory;
hPlx->addrDesc[ad_sp].dwMask = 0;
for (j=1; j<hPlx->addrDesc[ad_sp].dwBytes && j!=0x80000000; j *= 2)
{
hPlx->addrDesc[ad_sp].dwMask =
(hPlx->addrDesc[ad_sp].dwMask << 1) | 1;
}
}
}
break;
case ITEM_INTERRUPT:
if (hPlx->Int.Int.hInterrupt) return FALSE;
hPlx->Int.Int.hInterrupt = pItem->I.Int.hInterrupt;
break;
}
}
// check that all the items needed were found
// check if interrupt found
if (hPlx->fUseInt && !hPlx->Int.Int.hInterrupt)
{
return FALSE;
}
// check that the registers space was found
if (!P9030_IsAddrSpaceActive(hPlx, P9030_ADDR_REG))
//|| hPlx->addrDesc[P9030_ADDR_REG].dwBytes!=P9030_RANGE_REG)
return FALSE;
// check that at least one memory space was found
// for (i = P9030_ADDR_SPACE0; i<=P9030_ADDR_EPROM; i++)
// if (P9030_IsAddrSpaceActive(hPlx, i)) break;
// if (i>P9030_ADDR_EPROM) return FALSE;
return TRUE;
}
void P9030_Close(P9030_HANDLE hPlx)
{
// disable interrupts
if (P9030_IntIsEnabled(hPlx))
P9030_IntDisable(hPlx);
// unregister card
if (hPlx->cardReg.hCard)
WD_CardUnregister(hPlx->hWD, &hPlx->cardReg);
// close WinDriver
WD_Close(hPlx->hWD);
free (hPlx);
}
BOOL P9030_IsAddrSpaceActive(P9030_HANDLE hPlx, P9030_ADDR addrSpace)
{
return hPlx->addrDesc[addrSpace].dwAddr!=0;
}
DWORD P9030_ReadReg (P9030_HANDLE hPlx, DWORD dwReg)
{
return P9030_ReadSpaceDWord(hPlx, P9030_ADDR_REG, dwReg);
}
void P9030_WriteReg (P9030_HANDLE hPlx, DWORD dwReg, DWORD dwData)
{
P9030_WriteSpaceDWord(hPlx, P9030_ADDR_REG, dwReg, dwData);
}
void P9030_SetMode (P9030_HANDLE hPlx, P9030_ADDR addrSpace, P9030_MODE mode, DWORD dwLocalAddr)
{
DWORD dwRegOffset = 4*(addrSpace-P9030_ADDR_SPACE0);
P9030_ADDR_DESC *addrDesc = &hPlx->addrDesc[addrSpace];
addrDesc->dwLocalBase = dwLocalAddr & ~addrDesc->dwMask;
addrDesc->dwLocalBase |= BIT0;
P9030_WriteReg (hPlx, P9030_LAS0BA + dwRegOffset, addrDesc->dwLocalBase);
}
BYTE P9030_ReadSpaceByte (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwOffset)
{
if (hPlx->addrDesc[addrSpace].fIsMemory)
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddrDirect + dwOffset;
BYTE *pByte = (BYTE *) dwAddr;
return *pByte;
}
else
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddr + dwOffset;
WD_TRANSFER trans;
BZERO(trans);
trans.cmdTrans = RP_BYTE;
trans.dwPort = dwAddr;
WD_Transfer (hPlx->hWD, &trans);
return trans.Data.Byte;
}
}
void P9030_WriteSpaceByte (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwOffset, BYTE data)
{
if (hPlx->addrDesc[addrSpace].fIsMemory)
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddrDirect + dwOffset;
BYTE *pByte = (BYTE *) dwAddr;
*pByte = data;
}
else
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddr + dwOffset;
WD_TRANSFER trans;
BZERO(trans);
trans.cmdTrans = WP_BYTE;
trans.dwPort = dwAddr;
trans.Data.Byte = data;
WD_Transfer (hPlx->hWD, &trans);
}
}
WORD P9030_ReadSpaceWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwOffset)
{
if (hPlx->addrDesc[addrSpace].fIsMemory)
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddrDirect + dwOffset;
WORD *pWord = (WORD *) dwAddr;
return *pWord;
}
else
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddr + dwOffset;
WD_TRANSFER trans;
BZERO(trans);
trans.cmdTrans = RP_WORD;
trans.dwPort = dwAddr;
WD_Transfer (hPlx->hWD, &trans);
return trans.Data.Word;
}
}
void P9030_WriteSpaceWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwOffset, WORD data)
{
if (hPlx->addrDesc[addrSpace].fIsMemory)
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddrDirect + dwOffset;
WORD *pWord = (WORD *) dwAddr;
*pWord = data;
}
else
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddr + dwOffset;
WD_TRANSFER trans;
BZERO(trans);
trans.cmdTrans = WP_WORD;
trans.dwPort = dwAddr;
trans.Data.Word = data;
WD_Transfer (hPlx->hWD, &trans);
}
}
DWORD P9030_ReadSpaceDWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwOffset)
{
if (hPlx->addrDesc[addrSpace].fIsMemory)
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddrDirect + dwOffset;
DWORD *pDword = (DWORD *) dwAddr;
return *pDword;
}
else
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddr + dwOffset;
WD_TRANSFER trans;
BZERO(trans);
trans.cmdTrans = RP_DWORD;
trans.dwPort = dwAddr;
WD_Transfer (hPlx->hWD, &trans);
return trans.Data.Dword;
}
}
void P9030_WriteSpaceDWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwOffset, DWORD data)
{
if (hPlx->addrDesc[addrSpace].fIsMemory)
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddrDirect + dwOffset;
DWORD *pDword = (DWORD *) dwAddr;
*pDword = data;
}
else
{
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddr + dwOffset;
WD_TRANSFER trans;
BZERO(trans);
trans.cmdTrans = WP_DWORD;
trans.dwPort = dwAddr;
trans.Data.Dword = data;
WD_Transfer (hPlx->hWD, &trans);
}
}
void P9030_ReadWriteSpaceBlock (P9030_HANDLE hPlx, DWORD dwOffset, PVOID buf,
DWORD dwBytes, BOOL fIsRead, P9030_ADDR addrSpace, P9030_MODE mode)
{
WD_TRANSFER trans;
DWORD dwAddr = hPlx->addrDesc[addrSpace].dwAddr + dwOffset;
BZERO(trans);
if (hPlx->addrDesc[addrSpace].fIsMemory)
{
if (fIsRead)
{
if (mode==P9030_MODE_BYTE) trans.cmdTrans = RM_SBYTE;
else if (mode==P9030_MODE_WORD) trans.cmdTrans = RM_SWORD;
else trans.cmdTrans = RM_SDWORD;
}
else
{
if (mode==P9030_MODE_BYTE) trans.cmdTrans = WM_SBYTE;
else if (mode==P9030_MODE_WORD) trans.cmdTrans = WM_SWORD;
else trans.cmdTrans = WM_SDWORD;
}
}
else
{
if (fIsRead)
{
if (mode==P9030_MODE_BYTE) trans.cmdTrans = RP_SBYTE;
else if (mode==P9030_MODE_WORD) trans.cmdTrans = RP_SWORD;
else trans.cmdTrans = RP_SDWORD;
}
else
{
if (mode==P9030_MODE_BYTE) trans.cmdTrans = WP_SBYTE;
else if (mode==P9030_MODE_WORD) trans.cmdTrans = WP_SWORD;
else trans.cmdTrans = WP_SDWORD;
}
}
trans.dwPort = dwAddr;
trans.fAutoinc = TRUE;
trans.dwBytes = dwBytes;
trans.dwOptions = 0;
trans.Data.pBuffer = buf;
WD_Transfer (hPlx->hWD, &trans);
}
void P9030_ReadSpaceBlock (P9030_HANDLE hPlx, DWORD dwOffset, PVOID buf,
DWORD dwBytes, P9030_ADDR addrSpace, P9030_MODE mode)
{
P9030_ReadWriteSpaceBlock (hPlx, dwOffset, buf, dwBytes, TRUE, addrSpace, mode);
}
void P9030_WriteSpaceBlock (P9030_HANDLE hPlx, DWORD dwOffset, PVOID buf,
DWORD dwBytes, P9030_ADDR addrSpace, P9030_MODE mode)
{
P9030_ReadWriteSpaceBlock (hPlx, dwOffset, buf, dwBytes, FALSE, addrSpace, mode);
}
BYTE P9030_ReadByte (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwLocalAddr)
{
DWORD dwOffset = hPlx->addrDesc[addrSpace].dwMask & dwLocalAddr;
P9030_SetMode (hPlx, addrSpace, P9030_MODE_BYTE, dwLocalAddr);
return P9030_ReadSpaceByte(hPlx, addrSpace, dwOffset);
}
void P9030_WriteByte (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwLocalAddr, BYTE data)
{
DWORD dwOffset = hPlx->addrDesc[addrSpace].dwMask & dwLocalAddr;
P9030_SetMode (hPlx, addrSpace, P9030_MODE_BYTE, dwLocalAddr);
P9030_WriteSpaceByte(hPlx, addrSpace, dwOffset, data);
}
WORD P9030_ReadWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwLocalAddr)
{
DWORD dwOffset = hPlx->addrDesc[addrSpace].dwMask & dwLocalAddr;
P9030_SetMode (hPlx, addrSpace, P9030_MODE_WORD, dwLocalAddr);
return P9030_ReadSpaceWord(hPlx, addrSpace, dwOffset);
}
void P9030_WriteWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwLocalAddr, WORD data)
{
DWORD dwOffset = hPlx->addrDesc[addrSpace].dwMask & dwLocalAddr;
P9030_SetMode (hPlx, addrSpace, P9030_MODE_WORD, dwLocalAddr);
P9030_WriteSpaceWord(hPlx, addrSpace, dwOffset, data);
}
DWORD P9030_ReadDWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwLocalAddr)
{
DWORD dwOffset = hPlx->addrDesc[addrSpace].dwMask & dwLocalAddr;
P9030_SetMode (hPlx, addrSpace, P9030_MODE_DWORD, dwLocalAddr);
return P9030_ReadSpaceDWord(hPlx, addrSpace, dwOffset);
}
void P9030_WriteDWord (P9030_HANDLE hPlx, P9030_ADDR addrSpace, DWORD dwLocalAddr, DWORD data)
{
DWORD dwOffset = hPlx->addrDesc[addrSpace].dwMask & dwLocalAddr;
P9030_SetMode (hPlx, addrSpace, P9030_MODE_DWORD, dwLocalAddr);
P9030_WriteSpaceDWord(hPlx, addrSpace, dwOffset, data);
}
void P9030_ReadWriteBlock (P9030_HANDLE hPlx, DWORD dwLocalAddr, PVOID buf,
DWORD dwBytes, BOOL fIsRead, P9030_ADDR addrSpace, P9030_MODE mode)
{
DWORD dwOffset = hPlx->addrDesc[addrSpace].dwMask & dwLocalAddr;
P9030_SetMode (hPlx, addrSpace, mode, dwLocalAddr);
P9030_ReadWriteSpaceBlock(hPlx, dwOffset, buf, dwBytes, fIsRead, addrSpace, mode);
}
void P9030_ReadBlock (P9030_HANDLE hPlx, DWORD dwLocalAddr, PVOID buf,
DWORD dwBytes, P9030_ADDR addrSpace, P9030_MODE mode)
{
P9030_ReadWriteBlock (hPlx, dwLocalAddr, buf, dwBytes, TRUE, addrSpace, mode);
}
void P9030_WriteBlock (P9030_HANDLE hPlx, DWORD dwLocalAddr, PVOID buf,
DWORD dwBytes, P9030_ADDR addrSpace, P9030_MODE mode)
{
P9030_ReadWriteBlock (hPlx, dwLocalAddr, buf, dwBytes, FALSE, addrSpace, mode);
}
BOOL P9030_IntIsEnabled (P9030_HANDLE hPlx)
{
if (!hPlx->fUseInt) return FALSE;
if (!hPlx->Int.hThread) return FALSE;
return TRUE;
}
void P9030_IntHandler (PVOID pData)
{
P9030_HANDLE hPlx = (P9030_HANDLE) pData;
P9030_INT_RESULT intResult;
intResult.dwCounter = hPlx->Int.Int.dwCounter;
intResult.dwLost = hPlx->Int.Int.dwLost;
intResult.fStopped = hPlx->Int.Int.fStopped;
intResult.dwStatusReg = hPlx->Int.Trans[0].Data.Dword;
hPlx->Int.funcIntHandler(hPlx, &intResult);
}
BOOL P9030_IntEnable (P9030_HANDLE hPlx, P9030_INT_HANDLER funcIntHandler)
{
DWORD dwIntStatus;
DWORD dwAddr;
if (!hPlx->fUseInt) return FALSE;
// check if interrupt is already enabled
if (hPlx->Int.hThread) return FALSE;
dwIntStatus = P9030_ReadReg (hPlx, P9030_INTCSR);
BZERO(hPlx->Int.Trans);
// This is a samlpe of handling interrupts:
// Two transfer commands are issued. First the value of the interrrupt control/status
// register is read. Then, a value of ZERO is written.
// This will cancel interrupts after the first interrupt occurs.
// When using interrupts, this section will have to change:
// you must put transfer commands to CANCEL the source of the interrupt, otherwise, the
// PC will hang when an interrupt occurs!
dwAddr = hPlx->addrDesc[P9030_ADDR_REG].dwAddr + P9030_INTCSR;
hPlx->Int.Trans[0].cmdTrans = hPlx->addrDesc[P9030_ADDR_REG].fIsMemory ? RM_DWORD : RP_DWORD;
hPlx->Int.Trans[0].dwPort = dwAddr;
hPlx->Int.Trans[1].cmdTrans = hPlx->addrDesc[P9030_ADDR_REG].fIsMemory ? WM_DWORD : WP_DWORD;
hPlx->Int.Trans[1].dwPort = dwAddr;
hPlx->Int.Trans[1].Data.Dword = dwIntStatus & ~BIT6; // put here the data to write to the control register
hPlx->Int.Int.dwCmds = 2;
hPlx->Int.Int.Cmd = hPlx->Int.Trans;
hPlx->Int.Int.dwOptions |= INTERRUPT_CMD_COPY;
// this calls WD_IntEnable() and creates an interrupt handler thread
hPlx->Int.funcIntHandler = funcIntHandler;
if (!InterruptThreadEnable(&hPlx->Int.hThread, hPlx->hWD, &hPlx->Int.Int, P9030_IntHandler, (PVOID) hPlx))
return FALSE;
// this physically enables interrupts
P9030_WriteReg (hPlx, P9030_INTCSR, dwIntStatus | BIT6);
return TRUE;
}
void P9030_IntDisable (P9030_HANDLE hPlx)
{
DWORD dwIntStatus;
if (!hPlx->fUseInt) return;
if (!hPlx->Int.hThread) return;
// this disables interrupts
dwIntStatus = P9030_ReadReg (hPlx, P9030_INTCSR);
P9030_WriteReg (hPlx, P9030_INTCSR, dwIntStatus & ~BIT6);
// this calls WD_IntDisable()
InterruptThreadDisable(hPlx->Int.hThread);
hPlx->Int.hThread = NULL;
}
void P9030_EEPROMDelay(P9030_HANDLE hPlx)
{
WD_SLEEP sleep;
BZERO (sleep);
sleep.dwMicroSeconds = 500;
WD_Sleep( hPlx->hWD, &sleep);
}
BOOL P9030_EEPROMValid(P9030_HANDLE hPlx)
{
return (P9030_ReadReg(hPlx, P9030_CNTRL) & BIT28)==BIT28;
}
void P9030_Sleep(P9030_HANDLE hPlx, DWORD dwMicroSeconds)
{
WD_SLEEP sleep;
BZERO (sleep);
sleep.dwMicroSeconds = dwMicroSeconds;
WD_Sleep( hPlx->hWD, &sleep);
}
BYTE P9030_EEPROMEnable(P9030_HANDLE hPlx, WORD addr)
{
BYTE old_val;
old_val = P9030_ReadByte(hPlx, P9030_ADDR_REG, P9030_PROT_AREA);
addr /= 4;
addr &= 0x7f;
P9030_WriteByte(hPlx, P9030_ADDR_REG, P9030_PROT_AREA, (BYTE)addr);
P9030_Sleep(hPlx, 10000);
return old_val * 4; //expand from dwords to bytes
}
void P9030_EEPROMDataReadWrite(P9030_HANDLE hPlx, BOOL fIsRead, PDWORD pdwData)
{
WD_PCI_CONFIG_DUMP pciCnf;
BZERO (pciCnf);
pciCnf.pciSlot = hPlx->pciSlot;
pciCnf.pBuffer = pdwData;
pciCnf.dwOffset = P9030_VPD_DATA;
pciCnf.dwBytes = 4;
pciCnf.fIsRead = fIsRead;
WD_PciConfigDump(hPlx->hWD,&pciCnf);
}
void P9030_EEPROMAddrReadWrite(P9030_HANDLE hPlx, BOOL fIsRead, PWORD pwAddr)
{
WD_PCI_CONFIG_DUMP pciCnf;
BZERO (pciCnf);
pciCnf.pciSlot = hPlx->pciSlot;
pciCnf.pBuffer = pwAddr;
pciCnf.dwOffset = P9030_VPD_ADDR;
pciCnf.dwBytes = 2;
pciCnf.fIsRead = fIsRead;
WD_PciConfigDump(hPlx->hWD,&pciCnf);
}
BOOL P9030_EEPROMReadDWord(P9030_HANDLE hPlx, DWORD dwOffset, PDWORD pdwData)
{
WORD wVal;
WORD wAddr;
int i ;
BOOL fEnd = FALSE ;
if (dwOffset % 4)
{
sprintf (P9030_ErrorString, "The offset is not a multiple of 4\n");
return FALSE;
}
wAddr = (((WORD)dwOffset) & (~BIT15)) ;
P9030_EEPROMAddrReadWrite(hPlx, FALSE, &wAddr);
P9030_Sleep(hPlx, 10000);
for (i=0; !fEnd && i<100; i++)
{
P9030_EEPROMAddrReadWrite(hPlx, TRUE, &wVal);
if (wVal & BIT15)
fEnd = TRUE;
P9030_Sleep(hPlx, 10000);
}
if (i==100)
{
sprintf (P9030_ErrorString, "Acknoledge to EEPROM read was not recived\n");
return FALSE;
}
P9030_EEPROMDataReadWrite(hPlx, TRUE, pdwData);
return TRUE;
}
BOOL P9030_EEPROMReadWord(P9030_HANDLE hPlx, DWORD dwOffset, PWORD pwData)
{
DWORD dwData;
DWORD dwAddr;
if (dwOffset % 2)
{
sprintf (P9030_ErrorString, "The offset is not even\n");
return FALSE;
}
dwAddr = dwOffset - (dwOffset % 4);
if (!P9030_EEPROMReadDWord(hPlx, dwAddr, &dwData))
return FALSE;
*pwData = (WORD) (dwData >> ((dwOffset % 4)*8));
return TRUE;
}
BOOL P9030_EEPROMWriteWord(P9030_HANDLE hPlx, DWORD dwOffset, WORD wData)
{
DWORD dwData;
DWORD dwAddr;
dwAddr = dwOffset - (dwOffset % 4);
if (!P9030_EEPROMReadDWord(hPlx, dwAddr, &dwData))
return FALSE;
switch (dwOffset % 4)
{
case 0:
dwData = (dwData & 0xffff0000) | wData;
break;
case 2:
dwData = (dwData & 0x0000ffff) | (wData << 16);
break;
default:
sprintf (P9030_ErrorString, "The offset is not even\n");
return FALSE;
}
return P9030_EEPROMWriteDWord(hPlx, dwAddr, dwData);
}
BOOL P9030_EEPROMWriteDWord(P9030_HANDLE hPlx, DWORD dwOffset, DWORD dwData)
{
DWORD dwReadback;
WORD wAddr;
WORD wVal;
int i;
BOOL fRet;
BOOL fEnd = FALSE ;
BOOL fReadOk = FALSE;
BYTE bEnableOffset;
if (dwOffset % 4)
{
sprintf (P9030_ErrorString, "The offset is not a multiple of 4\n");
return FALSE;
}
wAddr = (WORD)dwOffset;
bEnableOffset = P9030_EEPROMEnable(hPlx, wAddr);
wAddr = wAddr | BIT15;
P9030_EEPROMDataReadWrite(hPlx, FALSE, &dwData);
P9030_EEPROMAddrReadWrite(hPlx, FALSE, &wAddr);
P9030_Sleep(hPlx, 10000);
for (i=0; !fEnd && i<100 ;i++)
{
P9030_EEPROMAddrReadWrite(hPlx, TRUE, &wVal);
if ((wVal & BIT15) == 0)
fEnd = TRUE;
P9030_Sleep(hPlx, 10000);
}
fReadOk = P9030_EEPROMReadDWord(hPlx, dwOffset, &dwReadback);
if (fReadOk && dwReadback==dwData)
fRet = TRUE;
else
{
fRet = FALSE;
if (fReadOk)
sprintf (P9030_ErrorString, "Write 0x%08x, Read 0x%08x\n",dwData, dwReadback);
else
sprintf (P9030_ErrorString, "Error reading EEPROM\n");
}
P9030_EEPROMEnable(hPlx, bEnableOffset);
return fRet;
}